package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.BootPageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionTextDao;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.BootPage;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionText;
import com.copm.demonstrationsystem.service.introductionfeatures.BootPageService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 18:50 2018/4/26
 */
@Service
public class BootPageServiceImpl implements BootPageService {

    private static Logger logger = LoggerFactory.getLogger(BootPageServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    BootPageDao bootPageDao;

    @Autowired
    ImageDao imageDao;

    @Autowired
    MultiImageAndTextHandle multiImageAndTextHandle;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    @Override
    public ResponseStatus getPage() {
        ResponseStatus status = new ResponseStatus();
        //以dto传输启动页信息
        BootPage bootPage = bootPageDao.findPage();
        if (null == bootPage) {
            status.setStatue(0);
            status.setMessage("没有“启动页”页面！");
            return status;
        }
        //获取启动页图片
        Image image = bootPage.getBackgroundImage();
        if (null == image) {
            bootPage.setBackgroundImage(null);
        } else {
            //更改图片路径
            image = transformImagePath.transformImagePath(image);
        }
        //获取logo
        Image logo = bootPage.getLogo();
        if (null == logo) {
            bootPage.setLogo(null);
        } else {
            //更改logo路径
            logo = transformImagePath.transformImagePath(logo);
        }
        //保存页面数据对象
        bootPage.setBackgroundImage(image);
        bootPage.setLogo(logo);
        status.setStatue(1);
        status.setData(bootPage);
        logger.info(" 引导页 页面信息成功获取！");
        return status;
    }

    @Override
    public ResponseStatus addBootPageImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“启动”页面对象
        BootPage bootPage = bootPageDao.findPage();
        //设置“启动”页面图片
        bootPage.setBackgroundImage(image);
        //保存已添加图片的对象
        bootPageDao.save(bootPage);
        status.setStatue(1);
        logger.info("添加图片成功！");
        return status;
    }

    @Override
    public ResponseStatus deleteBootPageImage() {
        ResponseStatus status;
        //获取页面对象
        BootPage bootPage = bootPageDao.findPage();
        //获取图片名
        Image image = bootPage.getBackgroundImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“启动”页面 ，设置图片为空
        bootPage.setBackgroundImage(null);
        //保存已删除的页面对象
        bootPageDao.save(bootPage);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info("图片删除成功！");
        return status;

    }

    @Override
    public ResponseStatus modifyBootPageImage(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        BootPage bootPage = bootPageDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == bootPage) {
            bootPage = new BootPage();
            //保存对象
            bootPageDao.save(bootPage);

        }
        if (null != bootPage.getBackgroundImage()) {
            status = deleteBootPageImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addBootPageImage(file);
        logger.info("图片修改成功！");
        return status;
    }

    @Override
    public ResponseStatus addBootPageLogo(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“启动”页面对象
        BootPage bootPage = bootPageDao.findPage();
        //设置“启动”页面图片
        bootPage.setLogo(image);
        //保存已添加图片的对象
        bootPageDao.save(bootPage);
        status.setStatue(1);
        logger.info("logo 添加成功！");
        return status;
    }

    @Override
    public ResponseStatus deleteBootPageLogo() {
        ResponseStatus status;
        //获取页面对象
        BootPage bootPage = bootPageDao.findPage();
        //获取图片名
        Image image = bootPage.getLogo();
        //物理路径删除图片
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“启动”页面 ，设置图片为空
        bootPage.setLogo(null);
        //保存已删除的页面对象
        bootPageDao.save(bootPage);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info("logo删除成功！");
        return status;
    }

    @Override
    public ResponseStatus modifyBootPageLogo(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        BootPage bootPage = bootPageDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == bootPage) {
            bootPage = new BootPage();
            //保存对象
            bootPageDao.save(bootPage);
        }
        if (null != bootPage.getLogo()) {
            status = deleteBootPageLogo();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addBootPageLogo(file);
        logger.info("logo修改成功！");
        return status;
    }

    @Override
    public ResponseStatus modifyBootPageText(String host, String address, String hotline) {
        ResponseStatus status = new ResponseStatus();
        //查询“企业简介”页面（只有一个页面数据）
        BootPage bootPage = bootPageDao.findPage();
        if (null == bootPage) {
            //新建“企业简介”页面对象
            bootPage = new BootPage();
        }
        //设置页面对象
        bootPage.setHost(host);
        bootPage.setAddress(address);
        bootPage.setHotline(hotline);
        //保存页面对象
        bootPageDao.save(bootPage);
        status.setStatue(1);
        logger.info("文字修改成功！");
        return status;
    }

}
